//
//  ViewController.m
//  01-瀑布流
//
//  Created by 李建舜 on 06/03/2017.
//  Copyright © 2017 李建舜. All rights reserved.
//

#import "ViewController.h"
#import "JSWaterflowLayout.h"
#import "JSShop.h"
#import "JSShopCell.h"
#import "MJExtension.h"
#import "MJRefresh.h"

@interface ViewController () <UICollectionViewDataSource, JSWaterflowLayoutDelegate>

/** 所有的商品数据 */
@property (nonatomic, strong) NSMutableArray *shops;

/** collectionView */
@property (nonatomic, weak) UICollectionView *collectionView;

@end

@implementation ViewController

#pragma mark - 懒加载
- (NSMutableArray *)shops {
    if (!_shops) {
        _shops = [NSMutableArray array];
    }
    return _shops;
}

static NSString * const JSShopId = @"shop";

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupLayout];
    
    [self setupRefresh];
    
}

- (void)setupLayout {
    // 创建布局
    JSWaterflowLayout *layout = [[JSWaterflowLayout alloc] init];
    layout.delegate = self;
    
    // 创建CollectionView
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    
    // 注册
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([JSShopCell class]) bundle:nil] forCellWithReuseIdentifier:JSShopId];
    
    self.collectionView = collectionView;

}

- (void)setupRefresh {
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewShops)];
    [self.collectionView.mj_header beginRefreshing];
    
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreShops)];
    self.collectionView.mj_footer.hidden = YES;
    
}


- (void)loadNewShops {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSMutableArray *shops = [JSShop mj_objectArrayWithFilename:@"1.plist"];
        [self.shops removeAllObjects];
        [self.shops addObjectsFromArray:shops];
        
        [self.collectionView reloadData];
        [self.collectionView.mj_header endRefreshing];
    });
}

- (void)loadMoreShops {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSMutableArray *shops = [JSShop mj_objectArrayWithFilename:@"1.plist"];
        [self.shops addObjectsFromArray:shops];
        
        [self.collectionView reloadData];
        [self.collectionView.mj_footer endRefreshing];
    });
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    self.collectionView.mj_footer.hidden = self.shops.count == 0;
    return self.shops.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    JSShopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:JSShopId forIndexPath:indexPath];
    cell.shop = self.shops[indexPath.item];
    
    return cell;
    
}

#pragma mark - JSWaterflowLayoutDelegate
- (CGFloat)waterflowLayout:(UICollectionViewLayout *)waterflowLayout heightForItemAtIndex:(NSInteger)index itemWidth:(CGFloat)itemWidth {
    JSShop *shop = self.shops[index];
    
    return itemWidth * shop.h / shop.w;
}


- (CGFloat)rowMarginInWaterflowLayout:(JSWaterflowLayout *)waterflowLayout
{
    return 15;
}

- (NSUInteger)columnCountInWaterflowLayout:(JSWaterflowLayout *)waterflowLayout {
    // 2列
    return 2;
}

- (UIEdgeInsets)edgeInsetsInWaterflowLayout:(JSWaterflowLayout *)waterflowLayout
{
    return UIEdgeInsetsMake(10, 20, 30, 100);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
